package karpiel.boguchwal.wojciech.przemek


object ValueDeclarationsHandler {

  sealed trait ValDecl {
    val name: String
  }

  case class ValDeclN(name: String, value: Ast) extends ValDecl

  case class ValDeclT(name: String, typeT: Ast, value: Ast) extends ValDecl

  def prepareContext(declarations: List[ValDecl], context: Context = Context.Empty)(implicit cache: Cache): Context = declarations match {
    case Nil => context
    case ValDeclN(name, value) :: tail =>
      val v = Bruinizer.bruinize(value)(context, implicitly)
      prepareContext(tail, context.append(v.inferType(context, implicitly).normalized(context, implicitly), v.normalized(context, implicitly), name))
    case ValDeclT(name, typeT, value) :: tail =>
      val v = Bruinizer.bruinize(value)(context, implicitly)
      val t = Bruinizer.bruinize(typeT)(context, implicitly)
      val vType = v.inferType(context, implicitly)
      vType.assertEquals(t)(context, implicitly)
      prepareContext(tail, context.append(t.normalized(context, implicitly), v.normalized(context, implicitly), name))
  }

}
